package com.duoketech.service.web.distribute;
import com.duoketech.model.mfood.MemberFood;
import com.duoketech.service.AbstractService;
import com.duoketech.model.distribute.FoodDistribute;
import com.duoketech.exception.ValidateException;
import com.duoketech.utils.search.LikeCondition;
import com.duoketech.utils.search.NotEqualCondition;
import com.duoketech.utils.search.QueryConstructor;
import com.duoketech.utils.validator.Validator;
import com.duoketech.utils.validator.rules.string.StringLengthBetween;
import com.duoketech.utils.validator.rules.string.StringMustBeRequired;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class FoodDistributeService extends AbstractService{
    public static FoodDistributeService ME = new FoodDistributeService();

    @Override
    public void validatorUpdate(Model<?> model, Kv kv) throws ValidateException {
        validatorAdd(model,kv);
    }

    @Override
    public void validatorAdd(Model<?> model, Kv kv) throws ValidateException {

        Validator validator = new Validator();
        validator.addRule(new StringMustBeRequired(model.getStr(FoodDistribute.NAME),"外卖订货人项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(FoodDistribute.NAME),"外卖订货人项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(FoodDistribute.ITEM),"外卖点餐内容项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(FoodDistribute.ITEM),"外卖点餐内容项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(FoodDistribute.MOBILE),"外卖订货人电话项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(FoodDistribute.MOBILE),"外卖订货人电话项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(FoodDistribute.SEND_ID),"外卖送货人项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(FoodDistribute.SEND_ID),"外卖送货人项字符必须为1-150个字符",1,150));
        validator.addRule(new StringMustBeRequired(model.getStr(FoodDistribute.ADDRESS),"外卖送货地址项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(FoodDistribute.ADDRESS),"外卖送货地址项字符必须为1-150个字符",1,150));
        validator.excute();

    }

    @Override
    public void beforAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public String tableName() {
        return FoodDistribute.dao.tableName();
    }



    public List<MemberFood> queryMemberFood(){
        String sql="select * from "+MemberFood.dao.tableName()+" order by id desc";
        return MemberFood.dao.find(sql);
    }

    @Override
    public void builderSQLExpected(Model<?> model, QueryConstructor queryConstruct, Kv kv) {

        setQuerySQLCommand("select t1.id,t1.name as sender,t2.* ");
        queryConstruct.setCommand(" from "+MemberFood.dao.tableName()+" t1,"+FoodDistribute.dao.tableName()+" t2 where t1.id=t2.send_id");
        queryConstruct.and(new NotEqualCondition("and t2."+FoodDistribute.STATUS,FoodDistribute.STATUS_DEL));
        queryConstruct.setOrderSql("order by t2.id desc");
        String value = model.getStr(FoodDistribute.MOBILE);
        if(StringUtils.isNotEmpty(value)){
            queryConstruct.and(new LikeCondition(" and t2."+FoodDistribute.MOBILE,"%"+value+"%"));
        }

    }
}